package com.jwcao.common.cast.operation.access.field;

import com.jwcao.common.cast.annotation.CastField;
import com.jwcao.common.cast.operation.CastFlow;
import com.jwcao.common.scan.object.ScanField;
import java.util.Map;

/**
 * name: GenericsAccessOperation description: TODO
 *
 * @author caojingwei date: 2023/3/23 18:42
 */
public class GenericsAccessOperation extends CastFieldAccessOperation {

    private final Map<Class, CastFlow> castFlowMap;

    public GenericsAccessOperation(CastField castField,
        ScanField sourceField, ScanField targetField, Map<Class, CastFlow> castFlowMap) {
        super(castField, sourceField, targetField);
        this.castFlowMap = castFlowMap;
    }

    @Override
    public Object execute(Object object) {
        Object value = super.execute(object);
        //进行类型推断
        if (value != null) {
            CastFlow castFlow = castFlowMap.get(value.getClass());
            return castFlow.execute(value);
        }
        return null;
    }
}
